package com.anyikang.volunteer.sos.volunteer.app.bean;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.anyikang.volunteer.sos.volunteer.app.MyApplication;

/**
 *
 * 我当前接的最新的任务单ID
 */

public class MyCurrentOrder {
    private String itemId;  //任务单ID
    int state; //1需要救助，2救助中，3已完成
    private String deviceIMEI;
    String sosPersonId;


    public String getSosPersonId() {
        return sosPersonId;
    }

    public void setSosPersonId(String sosPersonId) {
        this.sosPersonId = sosPersonId;
    }

    public String getDeviceIMEI() {
        return deviceIMEI;
    }

    public void setDeviceIMEI(String deviceIMEI) {
        this.deviceIMEI = deviceIMEI;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }


    public MyCurrentOrder()
    {
        return;
    }

    public MyCurrentOrder(Context context)
    {

        SharedPreferences sp = context.getSharedPreferences("my_cur_order", Context.MODE_PRIVATE);
        this.itemId = sp.getString("itemId", "");
        this.state = sp.getInt("state",0); // 0是不存在的
        this.deviceIMEI = sp.getString("deviceIMEI","");
        this.sosPersonId = sp.getString("sosPersonId","");

    }

    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    public void saveCurOrderInfo(Context context ,ContentValues curOrderValues)
    {
        if(curOrderValues != null)
        {
            SharedPreferences curOrderPreference = context.getSharedPreferences("my_cur_order", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = curOrderPreference.edit();
             itemId = curOrderValues.getAsString("itemId");
            state = curOrderValues.getAsInteger("state");
            deviceIMEI = curOrderValues.getAsString("deviceIMEI");
            sosPersonId = curOrderValues.getAsString("sosPersonId");

            //可以一次保存一个值或多个值，取决于ContentValues里的各个值是否为空
           // if(!TextUtils.isEmpty(itemId))
            {
                if(!TextUtils.isEmpty(itemId))
                {
                    edit.putString("itemId", itemId);
                }

                if(state != -1)  //-1表示无数据
                {
                    edit.putInt("state", state);
                }

                if(!TextUtils.isEmpty(deviceIMEI))
                {
                    edit.putString("deviceIMEI", deviceIMEI);
                }

                if(!TextUtils.isEmpty(sosPersonId))
                {
                    edit.putString("sosPersonId",sosPersonId);
                }

            }
            edit.commit();
        }
    }


    /**
     * 确认完成
     * @param context
     */
    public void sureComplete(Context context)
    {
        SharedPreferences curOrderPreference = context.getSharedPreferences("my_cur_order", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = curOrderPreference.edit();
        //gxw-edit.putString("itemId", "");
        edit.putInt("state", 3);
       //gxw- edit.putString("deviceIMEI", "");
        //gxw-edit.putString("sosPersonId","");
        edit.commit();
    }


    /**
     * 重新登录的话，会清除本地缓存，因为当用别人的账号登录，如果再用你上次自己的任务单缓存的话会有问题，因为志愿者账号已经变了
     * @param context
     */
    public void reset(Context context)
    {
        SharedPreferences curOrderPreference = context.getSharedPreferences("my_cur_order", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = curOrderPreference.edit();
        edit.putString("itemId", "");
        edit.putInt("state", 0);
        edit.putString("deviceIMEI", "");
        edit.putString("sosPersonId","");
        edit.commit();
        MyApplication.isRecvingOrder = false;
    }

}
